import { message } from 'ant-design-vue'
import { ref, watch } from 'vue'

import { userApi } from '@/apis'
import { useInputFormat } from '@/hooks'
type Params = {
  refresh: PromiseFunc
}
export function useGiveScore({ refresh }: Params) {
  const showChange = ref(false)
  const giveScoreNum = ref('')
  const reason = ref('')
  const userId = ref(0)
  const { formatInt } = useInputFormat()
  function showModal(id: number) {
    giveScoreNum.value = ''
    reason.value = ''
    showChange.value = true
    userId.value = id
  }
  function handleGiveScore() {
    if (!giveScoreNum.value) {
      message.error('请输入赠送积分')
      return
    }
    if (!reason.value) {
      message.error('请输入赠送理由')
      return
    }
    refresh(async () => {
      await userApi.giveStore(userId.value, giveScoreNum.value, reason.value)
      showChange.value = false
      message.success('赠送成功')
    })
  }
  watch(giveScoreNum, (val, preVal) => {
    formatInt(val, preVal, giveScoreNum)
  })
  return {
    showChange,
    giveScoreNum,
    reason,
    handleGiveScore,
    showModal
  }
}
